using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using HyperLocal.Core;
using HyperLocal.Service;
using HyperLocal.MVC.Controllers.Helpers;

namespace HyperLocal.MVC.Controllers
{
    public class CommunityController : Controller
    {


        //
        // GET: /Community/
        [AdminVerification(AdminType = AdminTypes.Super)]
        public ActionResult Index()
        {
            Repository repo = ConnectionService.getRepository;
            return View(repo.All<LocalOrbCommunity>());
        }

        //
        // GET: /Community/Details/5
        [AdminVerification(AdminType = AdminTypes.Super)]
        public ActionResult Details(Guid id)
        {
            Repository repo = ConnectionService.getRepository;
            var record = repo.First<LocalOrbCommunity>(x => x.LocalOrbCommunityId == id);
            return View(record);
        }

        //
        // GET: /Community/Create
        [AdminVerification(AdminType = AdminTypes.Super)]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Community/Create
        [AdminVerification(AdminType = AdminTypes.Super)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            Repository repo = ConnectionService.getRepository;

            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Community/Edit/5
        [AdminVerification(AdminType = AdminTypes.Super)]
        public ActionResult Edit(Guid id)
        {
            Repository repo = ConnectionService.getRepository;
            var record = repo.First<LocalOrbCommunity>(x => x.LocalOrbCommunityId == id);
            return View(record);
        }

        //
        // POST: /Community/Edit/5

        [AdminVerification(AdminType = AdminTypes.Super)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            Repository repo = ConnectionService.getRepository;
            var record = repo.First<LocalOrbCommunity>(x => x.LocalOrbCommunityId == id);

            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }

        }

        #region NON Admin Methods

        [Authorize]
        public ActionResult JoinCommunity()
        {
            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult JoinCommunity(FormCollection collection)
        {
            Repository repo = ConnectionService.getRepository;


            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        } 

        #endregion
    }


}
